package com.moyun.finance.service;

import com.moyun.finance.entity.Bill;
import com.moyun.finance.entity.ReimbursementDetailed;
import com.moyun.finance.entity.Response;
import com.moyun.finance.entity.Reimbursement;
import com.moyun.finance.repository.*;
import com.moyun.finance.utils.BaseUtil;
import com.moyun.finance.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ReimbursementService {
    @Autowired
    ReimbursementRepository rr;
    @Autowired
    ProjectsRepository pr;
    @Autowired
    OrgUserRepository our;
    @Autowired
    BillRepository br;
    @Autowired
    BillService bs;
    @Autowired
    AccountRepository ar;
    @Autowired
    ReimbursementDetailedService rds;

    public Response save(Reimbursement reimbursement){
        reimbursement.setId(BaseUtil.uuid());
        reimbursement.setState(0);//默认未报销
        reimbursement.setAlreadyMoney(0.0);//已报销
        reimbursement.setSurplusMoney(reimbursement.getMoney());//剩余报销
        rr.save(reimbursement);
        List<ReimbursementDetailed> rdss = reimbursement.getReimbursementDetaileds();
        for(ReimbursementDetailed rd : rdss){
            rd.setReimbursementDate(reimbursement.getDate());
        }
        rds.save(rdss, reimbursement.getId());
        return new Response("添加成功!",true);
    }

    public Response update(Reimbursement r){
        rr.save(r);//报销单存盘
        rds.update(r.getReimbursementDetaileds());
        return new Response("修改成功!",true);
    }

    public Response delete(Reimbursement reimbursement){
        rr.delete(reimbursement);
        return new Response("删除成功",true);
    }

    public Reimbursement findOneById(String reimbursementId){
        Reimbursement r = rr.findById(reimbursementId).orElse(null);
        r.setProjects(pr.findById(r.getProjectId()).orElse(null));
        r.setUser(our.findById(r.getUserId()).orElse(null));
        r.setReimbursementDetaileds(rds.findByReimbursementId(reimbursementId));
        r.setTransactor((r.getTransactorId()!=null) ? our.findById(r.getTransactorId()).orElse(null) : null);
        return r;
    }

    //本公司所有
    public List<Reimbursement> findAll(String companyId){
        List<Reimbursement> allRei =  rr.findByCompanyIdAndState(companyId, 0);
        for(Reimbursement r : allRei){
            r.setProjects(pr.findById(r.getProjectId()).orElse(null));
            r.setUser(our.findById(r.getUserId()).orElse(null));
            r.setTransactor((r.getTransactorId()!=null) ? our.findById(r.getTransactorId()).orElse(null) : null);
        }
        return allRei;
    }

    //报销确认支付
    public Response confirm(Reimbursement r){
        Reimbursement oldR = rr.findById(r.getId()).orElse(null);
        BigDecimal oldAlreadyMoney = new BigDecimal(oldR.getAlreadyMoney());//旧已报销
        BigDecimal newAlreadyMoney = new BigDecimal(r.getAlreadyMoney());//这次报销的
        BigDecimal alreadyMoney = oldAlreadyMoney.add(newAlreadyMoney);//新的已报销
        BigDecimal receivablesMoney = new BigDecimal(r.getMoney());//应报销
        BigDecimal surplusMoney = receivablesMoney.subtract(alreadyMoney);//剩余应报销
        r.setAlreadyMoney(alreadyMoney.doubleValue());
        r.setSurplusMoney(surplusMoney.doubleValue());
        r.setAlreadyDate(DateUtils.formatDate(new Date()));
        if(surplusMoney.doubleValue() <= 0){
            r.setState(1);//已经报销
        }
        //新增流水
        Bill bill = new Bill();
        bill.setBillMoney(newAlreadyMoney.doubleValue());
        bill.setReimbursementId(r.getId());
        bill.setCompanyId(r.getCompanyId());
        bill.setAccountId(r.getAccountId());
        bill.setBillType(0);
        bill.setProjectsId(r.getProjectId());
        bill.setUserId(r.getUserId());
        bs.save(bill);
        rr.save(r);
        return new Response("报销支付成功",true);
    }
}
